//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.Validation
{
	/// <summary>
	/// Multiple RuleSet Validator
	/// </summary>
	public class MultipleRuleSetValidator : Validator
	{
		#region Fields
		private string configurationFile;
		private IEnumerable<string> ruleSets;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the message template to use when logging results no message is supplied.
		/// </summary>
		/// <value></value>
		protected override string DefaultMessageTemplate
		{
			get { return null; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="MultipleRuleSetValidator"/> class.
		/// </summary>
		/// <param name="targetConfigurationFile">The target configuration file.</param>
		/// <param name="targetRuleSets">The target rule sets.</param>
		public MultipleRuleSetValidator(string targetConfigurationFile, IEnumerable<string> targetRuleSets)
			: base(null, null)
		{
			this.configurationFile = targetConfigurationFile;
			this.ruleSets = targetRuleSets;
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Implements the validation logic for the receiver.
		/// </summary>
		/// <param name="objectToValidate">The object to validate.</param>
		/// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
		/// <param name="key">The key that identifies the source of <paramref name="objectToValidate"/>.</param>
		/// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
		/// <remarks>
		/// Subclasses must provide a concrete implementation the validation logic.
		/// </remarks>
		public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
		{
			if (objectToValidate != null)
			{
				Type targetType = objectToValidate.GetType();
				foreach (string ruleSet in ruleSets)
				{
					Validator v = ValidationFactory.CreateValidator(targetType, ruleSet, new FileConfigurationSource(configurationFile));
					v.Validate(objectToValidate, validationResults);
				}
			}
		}
		#endregion
	}
}
